package com.zj.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.dao.AddressMapper;
import com.zj.model.Address;
import com.zj.restApi.IdGenratorApi;
import com.zj.web.bean.AddressDTO;
import com.zj.web.bean.ResponseResult;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Log
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService{
    private final IdGenratorApi idGenratorApi;
    private final AddressMapper addressMapper;
    //添加地址
    @Override
    public AddressDTO addAddress(AddressDTO addressDTO) {
        //1.调用 idGenerator 服务 取出id号
        ResponseEntity<ResponseResult<Long>> responseEntity = idGenratorApi.getNextId();  //jdk动态代理对象
        if(responseEntity.getStatusCode()!= HttpStatus.OK || responseEntity.getBody()==null){
            throw new RuntimeException("调用idGenerator服务失败");
        }
        ResponseResult<Long> responseResult = responseEntity.getBody();
        log.info(responseResult.toString());
        if (responseResult == null || responseResult.getCode() != 1) {
            throw new RuntimeException("调用idGenerator服务失败");
        }
        Long id = responseResult.getData();
        addressDTO.setAddressId(id);

        addressDTO.setIsDefault(ADDRESS_NOTDEFAULT);
        addressDTO.setAddressStatus(ADDRESS_STATUS_ENABLE);

        Address address=new Address();
        BeanUtils.copyProperties(addressDTO,address);
        boolean flag=this.save(address);
        if(!flag){
            throw new RuntimeException("添加地址失败");
        }
        return addressDTO;
    }

    //根据状态和用户Id查找地址
    @Override
    public List<AddressDTO> getAddressesByStatus(Integer address_status,Long user_id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getAddressStatus,address_status);
        wrapper.eq(Address::getUserId,user_id);
        //倒序
        wrapper.orderByDesc(Address::getAddressId);
        return addressMapper.selectList(wrapper).stream().map(address -> {
            AddressDTO addressDTO = new AddressDTO();
            BeanUtils.copyProperties(address,addressDTO);
            return addressDTO;
        }).collect(Collectors.toList());
    }

    //根据索引和用户Id更新为是否是默认地址
    @Override
    public boolean updateIsDefault(Long address_id, Integer is_default,Long user_id) {
        //将所有地址的默认状态设为0
        getAddressesByStatus(ADDRESS_STATUS_ENABLE,user_id).forEach(address -> {   //查找地址状态为0的地址数据
            address.setIsDefault(0);  //将默认状态设为0
            Address address1=new Address();
            BeanUtils.copyProperties(address,address1);
            addressMapper.updateById(address1); //更新地址数据
        });
        Address address = addressMapper.selectById(address_id);
        if(address==null){
            throw new RuntimeException("地址不存在"+address_id);
        }
        address.setIsDefault(is_default);
        return addressMapper.updateById(address)>0;
    }

    //根据索引改变地址的状态
    @Override
    public boolean updateAddressStatus(Long address_id, Integer address_status) {
        Address address = this.addressMapper.selectById(address_id);
        if(address==null){
            throw new RuntimeException("地址不存在"+address_id);
        }
        if(address_status==0){
            address.setIsDefault(0);
        }
        address.setAddressStatus(address_status);
        return this.addressMapper.updateById(address)>0;
    }

    //根据索引修改地址信息
    @Override
    public boolean updateAddress(Long address_id, AddressDTO addressDTO) {
        Address address = addressMapper.selectById(address_id);
        if(address==null){
            throw new RuntimeException("地址不存在"+address_id);
        }
        BeanUtils.copyProperties(addressDTO,address);
        return addressMapper.updateById(address)>0;
    }

    //根据用户Id查找默认地址的地址信息
    @Override
    public AddressDTO getDefaultAddress(Integer is_default,Integer address_status,Long user_id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,user_id);
        wrapper.eq(Address::getIsDefault,is_default);
        wrapper.eq(Address::getAddressStatus,address_status);
        Address address = addressMapper.selectOne(wrapper);
        AddressDTO addressDTO = new AddressDTO();
        if(address==null){
            return null;
        }
        BeanUtils.copyProperties(address,addressDTO);
        return addressDTO;
    }
}
